$(function () {
    $("#jqGrid").Grid({
        url: '../tbVideo/list',
        colModel: [
            {label: 'id', name: 'id', index: 'id', key: true, hidden: true},
            {label: '分类', name: 'categoryName', width: 30},
            {label: '名字', name: 'name', index: 'name', width: 30},
            {label: '封面', name: 'cover', index: 'cover', width: 30,formatter:function (value) {
                    if (value===null){
                        return "图片处理中"
                    } else {
                        return '<img src="' + value+ '"  style="width:50px;height:50px;" />'
                    }
                }},
            {
                label: '是否热门', name: 'isHot', index: 'isHot', width: 30, formatter: function (value) {
                    if (value === 1) {
                        return "是"
                    } else {
                        return "否"
                    }
                }
            },
            {
                label: '是否推荐', name: 'isRecommend', index: 'isRecommend', width: 30, formatter: function (value) {
                    if (value === 1) {
                        return "是"
                    } else {
                        return "否"
                    }
                }
            },

            {label: '视频', name: 'url', index: 'url', width: 80, hidden: true},
        ]
    });
})

let vm = new Vue({
    el: '#rrapp',
    data: {
        insert:false,
        showCategory: false,
        showList: true,
        title: null,
        tbVideo: {},
        ruleValidate: {
            name: [
                {required: true, message: '名称不能为空', trigger: 'blur'}
            ]
        },
        q: {
            name: ''
        },
        category: [],
    },
    methods: {
        playVideo: function () {
            let id = getSelectedRowData("#jqGrid");
            let player = window.open('', '', 'width=600,height=490');
            player.document.write('<iframe id="cciframe_86DFC293F0297FF99C33DC5901307461" src="https://p.bokecc.com/playhtml.bo?vid=' + id.url + '&siteid=DE2AB7614DC4B83C&autoStart=false&playerid=7FE43EB140A9BA71&playertype=1" frameborder="0" height="490" width="600"></iframe>');
            player.focus();
        },


        query: function () {
            vm.reload();
        },
        add: function () {
            vm.insert=true;
            vm.showCategory=true;
            vm.showList = false;
            vm.title = "新增";
            vm.tbVideo = {
                sort:1,
                isHot:0,
                isRecommend:0,
            };
            this.getVideoCategory();
        },
        update: function (event) {
            vm.insert=false;
            let id = getSelectedRow("#jqGrid");
            if (id == null) {
                return;
            }
            vm.showCategory=true;
            vm.showList = false;
            vm.title = "修改";
            this.getVideoCategory();
            vm.getInfo(id);
            ;
        },
        getInfo: function (id) {
            Ajax.request({
                url: "../tbVideo/info/" + id,
                async: true,
                successCallback: function (r) {
                    vm.tbVideo = r.tbVideo;
                }
            });
        },
        saveOrUpdate: function (event) {
            let url = vm.tbVideo.id == null ? "../tbVideo/save" : "../tbVideo/update";
            Ajax.request({
                url: url,
                params: JSON.stringify(vm.tbVideo),
                type: "POST",
                contentType: "application/json",
                successCallback: function (r) {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                }
            });
        },
        del: function (event) {
            let ids = getSelectedRows("#jqGrid");
            if (ids == null) {
                return;
            }

            confirm('确定要删除选中的记录？', function () {
                Ajax.request({
                    url: "../tbVideo/delete",
                    params: JSON.stringify(ids),
                    type: "POST",
                    contentType: "application/json",
                    successCallback: function () {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    }
                });
            });
        },
        reload: function (event) {
            vm.insert=false;
            vm.showList = true;
            let page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {'name': vm.q.name},
                page: page
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
        },
        reloadSearch: function () {
            vm.q = {
                name: ''
            }
            vm.reload();
        },
        handleSubmit: function (name) {
            vm.tbVideo.url = uploadVideo();
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        },
        getVideoCategory: function () {
            Ajax.request({
                url: "../tbVideoCategory/all",
                successCallback: function (r) {
                    vm.category = r.category;
                }
            });
        },
        handleMaxSize: function (file) {
            this.$Notice.warning({
                title: '超出文件大小限制',
                desc: '文件 ' + file.name + ' 太大，不能超过 2M。'
            });
        },
        handleSuccessListPicUrl: function (res, file) {
            vm.tbVideo.cover = file.response.url;
        },
        eyeImagePicUrl: function () {
            var url = vm.tbVideo.cover;
            eyeImage(url);
        },
        eyeImage: function (e) {
            eyeImage($(e.target).attr('src'));
        },
        handleRemove: function (file) {
            // 从 upload 实例删除数据
            const fileList = this.uploadList;
            this.uploadList.splice(fileList.indexOf(file), 1);
        },
        handleSuccess: function (res, file) {
            // 因为上传过程为实例，这里模拟添加 url
            file.imgUrl = res.url;
            file.name = res.url;
            vm.uploadList.add(file);
        },
        handleSuccessPicUrl: function (res, file) {
            vm.tbVideo.cover = file.response.url;
        },

        handleBeforeUpload: function () {
            const check = this.uploadList.length < 5;
            if (!check) {
                this.$Notice.warning({
                    title: '最多只能上传 5 张图片。'
                });
            }
            return check;
        },
        handleFormatError: function (file) {
            this.$Notice.warning({
                title: '文件格式不正确',
                desc: '文件 ' + file.name + ' 格式不正确，请上传 jpg 或 png 格式的图片。'
            });
        },
    }
});


